import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable()
export class RemoveTableService {

  public getRemoveTableId	: Array<string> = [];
  constructor(
    private http : Http
  ) {}

  /**
   * 全删
   * @param bool 
   * @param tabVal 
   * @param id 
   */ 
  removeAll(bool, tabVal, id):boolean {
  	if(bool) {
  		this.getRemoveTableId = [];
  		tabVal.forEach((obj, i, object)=>{
  			this.getRemoveTableId[i] = obj[id];
      });
  		return true;
  	}
  	else {
  		this.getRemoveTableId = [];
  		return false;
  	}
  }
/**
   * 全删
   * @param bool 
   * @param tabVal 
   * @param id 
   */ 
  keywordRemoveAll(bool, tabVal, id):boolean {
  	if(bool) {
  		this.getRemoveTableId = [];
  		tabVal.forEach((obj, i, object)=>{
  			this.getRemoveTableId[i] = ""+obj[id]+"";
      });
  		return true;
  	}
  	else {
  		this.getRemoveTableId = [];
  		return false;
  	}
  }
  /**
   * 单删（多删）
   * @param bool 
   * @param choseId 
   * @param TotalLength 
   */ 
  removeChose(bool, choseId, TotalLength): any {   
  	if(bool) {
      this.getRemoveTableId.push(choseId);
  	}
  	else {
  		this.rmSame(this.getRemoveTableId, choseId);
  	}
  	if(TotalLength != this.getRemoveTableId.length) {
      return { 
               bool : false, 
               len  : this.getRemoveTableId.length 
             };
  	}
  	else {
  		return { 
               bool : true, 
               len  : this.getRemoveTableId.length 
             };
  	}
  }

  /**
   * 单删（多删）
   * @param bool 
   * @param choseId 
   * @param TotalLength 
   */ 
  keywordRemoveChose(bool, choseId, TotalLength): any {   
  	if(bool) {
      this.getRemoveTableId.push(""+choseId+"");
  	}
  	else {
  		this.rmSame(this.getRemoveTableId, choseId);
  	}
  	if(TotalLength != this.getRemoveTableId.length) {
      return { 
               bool : false, 
               len  : this.getRemoveTableId.length 
             };
  	}
  	else {
  		return { 
               bool : true, 
               len  : this.getRemoveTableId.length 
             };
  	}
  }

  /**
   * 去重
   * @param arr 
   * @param val 
   */ 
  rmSame(arr, val) { 
    for(let i in arr) {
      if(arr[i]==val) {
        arr.splice(i, 1);
      }
    }
  }

  /**
   * 确认删
   * @param id 
   * @param url 
   * @param optionIdKey 
   * @param optionIdVal 
   */ 
  confirmRemove(id, url, optionIdKey?, optionIdVal?) {
  	
    let data = {};

    if(optionIdKey) {
      data = { [id] : this.getRemoveTableId, [optionIdKey] : optionIdVal };
    }
    else {
      data = { [id] : this.getRemoveTableId };
    }

    return this.http
           .post(url, data)
           .toPromise()
           .then(res => {
             this.getRemoveTableId = [];
             return res.json();
           })
           .catch(err=>err)
  }
/**
   * 确认启用或禁用
   * @param id 
   * @param url 
   * @param optionIdKey 
   * @param optionIdVal 
   */ 
  confirmEnableDisable(tokenVal,id, url, optionIdKey?, optionIdVal?) {
  	
    let data = {};

    if(optionIdKey) {
      data = { ["token"]: tokenVal, [id] : this.getRemoveTableId, [optionIdKey] : optionIdVal };
    }
    else {
      data = { ["token"]: tokenVal, [id] : this.getRemoveTableId };
    }
    return this.http
           .post(url, data)
           .toPromise()
           .then(res => {
             this.getRemoveTableId = [];
             return res.json();
           })
           .catch(err=>err)
  }
  /**
   * 所有ID
   * @param id 
   */ 
  _AllId(id) {
    return {
      [id]: this.getRemoveTableId
    }
  }

  /**
   * 清空ID
   */ 
  _clearId(id) {
    this.getRemoveTableId = [];
    return {
      [id]: this.getRemoveTableId
    }
  }

  /**
   * 判断是否为空数组
   */ 
  _isEmpty() {
    if (this.getRemoveTableId.length==0) {
      return true;
    }
    return false;
  }

}


